package com.epfresh.supplier.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.RequestEntityMapSupply;
import com.epfresh.api.entity.RequestTag;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.utils.CustomDialog;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.api.utils.T;
import com.epfresh.api.utils.ToastUtils;
import com.epfresh.api.utils.keyBoardUtil;
import com.epfresh.api.widget.CommonInputItem;
import com.epfresh.api.widget.SlideSwitch;
import com.epfresh.api.widget.TagCloudView;
import com.epfresh.api.widget.keyboard.KeyboardUtil;
import com.epfresh.supplier.R;
import com.epfresh.supplier.constant.Constant;
import com.epfresh.supplier.entity.CertInfo;
import com.epfresh.supplier.entity.MerChantInfo;
import com.epfresh.supplier.entity.Store;
import com.epfresh.supplier.entity.StoreBaseInfo;
import com.epfresh.supplier.entity.StoreInfo;
import com.epfresh.supplier.entity.updateDeliveryMinAmountReponse;
import com.epfresh.supplier.widget.IosAlertDialog;
import com.google.gson.Gson;
import com.tencent.mmkv.MMKV;

import java.util.HashMap;
import java.util.Map;

public class StoreEnsureAndOpenActivity extends BaseActivity {

    private TextView tvStoreName;
    private TextView tvStoreStatus;
    private ImageView ivStoreName;
    private ImageView ivNameLogo;
    private View vStore;
    private View vArrow;

    private TagCloudView tvtTag;

    private View vOrder;
    private View vOffline;
    private SlideSwitch ssOrder;
    private SlideSwitch ssOffline;

    private CommonInputItem ciStore;
    private CommonInputItem ciName;
    private CommonInputItem ciBank;
    private CommonInputItem ciPickArreess;
    private CommonInputItem ciManagerInfo;
    private CommonInputItem ciStartAmount;//起送金额


    String certStatus = "", certType = "", auditStatus = "";
    String bankCartType = "";

    int cardCount = -1;

    View vToolbar;

    private Handler handler = new Handler();
    private String startMoney;//起送金额的值

    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        outState.putString("certInfo", getIntent().getStringExtra("certInfo"));
//        outState.putBoolean("isFirst",getIntent().getBooleanExtra("isFirst",false));
        super.onSaveInstanceState(outState);
    }

    /*
            <!--已认证 未通过，重填 未填写-->
        */
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
//        updateStoreStatus();
        requestStoreInfo();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
//        updateStoreStatus();
        requestStoreInfo();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        requestStoreInfo();
    }

    @Override
    protected View getToolBar() {
        return null;
    }

    void initView() {
        setContentView(R.layout.activity_store_ensure_and_open);
        vToolbar = findViewById(R.id.ll_toolbar);
        toolbarTitle = (TextView) findViewById(R.id.toolbar_title);
        rightbtn = (TextView) findViewById(R.id.rightbtn);
        ivBack = (ImageView) findViewById(R.id.baseback);
        vToolbar.setBackgroundColor(0x00ffffff);
        ivBack.setImageResource(R.mipmap.shade_arrow);

        toolbarTitle.setText("");
        ivBack.setVisibility(View.VISIBLE);
        rightbtn.setText(getString(R.string.help));
//        rightbtn.setTextColor(0xff898989);
        rightbtn.setTextColor(0xffffffff);
        rightbtn.setVisibility(View.VISIBLE);
        rightbtn.setOnClickListener(this);


        vStore = findViewById(R.id.rl_add_store);
        tvStoreName = (TextView) findViewById(R.id.tv_store_name);
        tvStoreStatus = (TextView) findViewById(R.id.tv_store_state);
        ivStoreName = (ImageView) findViewById(R.id.iv_store_name);
        ivNameLogo = (ImageView) findViewById(R.id.iv_name_logo);
        vArrow = findViewById(R.id.iv_store_arrow);

        ciStore = (CommonInputItem) findViewById(R.id.ci_store);
        ciName = (CommonInputItem) findViewById(R.id.ci_name);
        ciBank = (CommonInputItem) findViewById(R.id.ci_bank);
        ciStartAmount = findViewById(R.id.ci_start_amount);
        ciPickArreess = (CommonInputItem) findViewById(R.id.ci_pick_address);
        ciManagerInfo = (CommonInputItem) findViewById(R.id.ci_manager_info);
        ciManagerInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StoreInfo storeInfo = getStoreInfo();

                if (storeInfo != null && BaseApplication.getInstance().isOwned()) {
                    Intent intent = new Intent(StoreEnsureAndOpenActivity.this, StoreMangerInfoActivity.class);
                    intent.putExtra("managerName", storeInfo.getManagerName());
                    intent.putExtra("managerPhone", storeInfo.getManagerPhone());
                    startActivityForResult(intent, 0x1995);
                }
            }
        });

        ciStore.setOnClickListener(this);
        ciName.setOnClickListener(this);
        ciBank.setOnClickListener(this);
        ciPickArreess.setOnClickListener(this);
        ciStartAmount.setOnClickListener(this);
        vStore.setOnClickListener(this);


        ssOrder = (SlideSwitch) findViewById(R.id.ss_receive);
        findViewById(R.id.v_offline).setOnClickListener(this);
        ssOffline = (SlideSwitch) findViewById(R.id.ss_offline);
        vOrder = findViewById(R.id.v_receive);
        vOrder.setOnClickListener(this);
        ssOrder.setSlideable(false);

        vOrder = findViewById(R.id.rl_receive_order);
        vOffline = findViewById(R.id.rl_order_offline);

        tvtTag = (TagCloudView) findViewById(R.id.tvt_tag);
    }


    void updateStoreStatus(StoreInfo storeInfo) {
        certStatus = storeInfo.getStoreStatus();
        bankCartType = storeInfo.getBankCardType();
        certType = storeInfo.getCertType();
        cardCount = storeInfo.getBankCardCount();
        Store store = storeInfo.getStore();
        if (store != null) {
            auditStatus = store.getAuditStatus();
        } else {
            auditStatus = "";
        }
        auditStatus = storeInfo.getStoreStatus();


        //实名认证

        ivNameLogo.setVisibility(View.GONE);

        TextView content = ciName.getContent();
        content.setVisibility(View.VISIBLE);
        if (certStatus == null || "".equals(certStatus) || certStatus.equals("0")) {
            ciName.setText(getString(R.string.no_cert), 0);
        } else if (certStatus.equals("1")) {
            ciName.setText(getString(R.string.in_cert), -1);
//            ciName.showFlag(false);
//            ciName.setClickable(false);
        } else if (certStatus.equals("2")) {
//            ciName.showFlag(false);
            ciName.setText(getString(R.string.has_cert), 1);
            ivNameLogo.setVisibility(View.INVISIBLE);
            if ("1".equals(storeInfo.getCertType())) {
                ivNameLogo.setImageResource(R.mipmap.pr_logo);
            } else if ("2".equals(storeInfo.getCertType())) {
                ivNameLogo.setImageResource(R.mipmap.co_logo);

            }


//            content.setVisibility(View.INVISIBLE);
//            ciName.setContentText("");
        } else {
            ciName.setText(getString(R.string.un_cert) + "，请修改信息", -1);
        }

        //店铺
       /* if (auditStatus == null||auditStatus.equals("")) {
            ciStore.setText(getString(R.string.no_cert),0);
        } else if (auditStatus.equals("0")) {
            ciStore.setText(getString(R.string.in_cert),-1);
        } else if (auditStatus.equals("1")) {
            ciStore.setText(getString(R.string.has_cert),1);
        } else if (auditStatus.equals("2")){
            ciStore.setText(getString(R.string.un_cert)+"，请修改信息",-1);
        }
*/
        //0,未开,1,开店,2,过,3,未过,4,冻结
        if (auditStatus == null || auditStatus.equals("") || auditStatus.equals("0")) {
            ciStore.setText(getString(R.string.no_cert), 0);
        } else if (auditStatus.equals("1")) {
            ciStore.setText(getString(R.string.in_cert), -1);
        } else if (auditStatus.equals("2")) {
            ciStore.setText(getString(R.string.has_cert), 1);
        } else if (auditStatus.equals("3")) {
            ciStore.setText(getString(R.string.un_cert) + "，请修改信息", -1);
        } else if (auditStatus.equals("4")) {
            ciStore.setText("冻结中", -1);
        }

        //银行卡
        if (cardCount > 0) {
            ciBank.setText("已填写", 0);

        } else {
            ciBank.setText(getString(R.string.no_edit), 0);
        }
//        ciBank.setText("已填写",0);

        if (store != null) {

            if (store.getHeaderImageUrl() != null && !"".equals(store.getHeaderImageUrl())) {
                ImageLoader.getInstance().displayImage(store.getHeaderImageUrl(), ivStoreName);
            }


            if (store.getName() != null) {
                tvStoreName.setText(store.getName());
                tvStoreStatus.setText("");
            }
        } else {
            tvStoreName.setText(R.string.store_state_none);
            tvStoreStatus.setText(R.string.store_edit);
        }
        if (storeInfo.isOpenStore()) {
//            ciStore.setVisibility(View.GONE);
            ciStore.setVisibility(View.VISIBLE);
            tvStoreStatus.setVisibility(View.INVISIBLE);
            ciPickArreess.setVisibility(View.VISIBLE);
            vOrder.setVisibility(View.VISIBLE);
            vOffline.setVisibility(View.GONE);
            tvtTag.setVisibility(View.VISIBLE);

            tvtTag.setCommTags(store.getTags());
            vArrow.setVisibility(View.GONE);

            if (storeInfo.isDc()) {
                findViewById(R.id.iv_pick).setVisibility(View.VISIBLE);
            } else {
                findViewById(R.id.iv_pick).setVisibility(View.GONE);

            }

        } else {
            tvStoreStatus.setVisibility(View.INVISIBLE);
            ciStore.setVisibility(View.VISIBLE);
            ciPickArreess.setVisibility(View.GONE);
            vOrder.setVisibility(View.GONE);
            vOffline.setVisibility(View.GONE);
            tvtTag.setVisibility(View.GONE);
            vArrow.setVisibility(View.GONE);
        }

        if (storeInfo.isOpenStore()) {
            tvStoreStatus.setText("");
        } else if (storeInfo.isStoreEmpty()) {
            tvStoreStatus.setText(R.string.store_state_none);
        } else if (storeInfo.isStoreReject()) {
            tvStoreStatus.setText(R.string.store_state_checked_fail);
        } else {
            tvStoreStatus.setText(R.string.store_state_checking);
        }


        ciManagerInfo.setContentText(storeInfo.getMangerInfo());
        if (storeInfo.isOpenStore()) {
            ciManagerInfo.setVisibility(View.VISIBLE);
            ssOrder.setState(store.isOpen());
            ssOffline.setState(store.isOffline());
        } else {
            ciManagerInfo.setVisibility(View.GONE);
        }
        //是否显示起送金额
        if (storeInfo.getStore().isSupplierDelivery()) {
            startMoney = storeInfo.getStore().getDeliveryMinAmount();
            ciStartAmount.setVisibility(View.VISIBLE);
            ciStartAmount.setContentText(startMoney + "元");
        } else {
            ciStartAmount.setVisibility(View.GONE);
        }
    }


    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.v_receive:

                dealSwitch();

                break;
            case R.id.v_offline:

                dealOffline();
                break;
            case R.id.rl_add_store:
//                if (getStoreInfo().isOpenStore()) {
////                    Intent intent = new Intent(this, .class);
////                    intent.putExtra(Constant.KEY_STORE_TYPE_STATUS, Constant.TYPE_STORE_STATUS_PRVIEW_EDIT);
////                    startActivity(intent);
//
//                    Intent intent = new Intent(this, StoreActivity.class);
////                auditStatus = "1";
//                    if (auditStatus.equals("1")) {
//                        ciStore.setText(getString(R.string.has_cert),1);
//                        intent.putExtra(Constant.KEY_STORE_TYPE_STATUS, Constant.TYPE_STORE_STATUS_PRVIEW_EDIT);
//                    }
//                    startActivityForResult(intent,0x1992);
//
//
//                }
                break;
            case R.id.ci_pick_address:
                Intent intent2 = new Intent(this, MyAddressActivity.class);
                startActivity(intent2);
                break;
            case R.id.ci_name:

//                Intent person = new Intent(this,
//                        SelectNameEnsureActivity.class);
//                startActivity(person);

//                if (certStatus.equals("0")) {
//                    if (certType == null) {//没有认证过
//                        Intent person = new Intent(this,
//                                SelectNameEnsureActivity.class);
//
//
////                        Intent intent = new Intent(this,
////                                EnsurePersonActivity.class);
////                        intent.putExtra("certType",getIntent().getStringExtra("certType"));
////                        intent.putExtra("certInfo",getIntent().getStringExtra("certInfo"));
////                        intent.putExtra("isFirst",getIntent().getBooleanExtra("isFirst",false));
//                        startActivity(person);
//                    } else if (certType.equals("1")) {//个人认证
//                        Intent person = new Intent(this,
//                                EnsurePersonActivity.class);
//                        startActivity(person);
//                    } else {
//                        Intent person = new Intent(this,
//                                SelectNameEnsureActivity.class);
//                        startActivity(person);
//                    }
//                } else if (certStatus.equals("1")) {
////                    T.toast("当前认证中，请耐心等待结果");
//                    getCertInfo();
//
//                } else if (certStatus.equals("3")) {//认证失败
////                    T.toast("当前认证中，请耐心等待结果");
//                    getCertInfo();
//                } else if (certStatus.equals("2")) {
//                    getCertInfo();
//                }
                break;
            case R.id.ci_store:
                MMKV.defaultMMKV().encode("toIndex", 2);
                MMKV.defaultMMKV().encode("isStore", true);
                Intent intent = new Intent(this, StoreActivity.class);
//                auditStatus = "";
                //0,未开,1,开店,2,过,3,未过,4,冻结
                if (auditStatus.equals("") || "0".equals(auditStatus)) {
                    //"未开店"
                    intent.putExtra(Constant.KEY_STORE_TYPE_STATUS, Constant.TYPE_STORE_STATUS_FIRST_OPEN);
                } else if (auditStatus.equals("1")) {

                    intent.putExtra(Constant.KEY_STORE_TYPE_STATUS, Constant.TYPE_STORE_STATUS_PRVIEW);
                } else if (auditStatus.equals("2")) {
//                    ciStore.setText(getString(R.string.has_cert),1);

                    intent.putExtra(Constant.KEY_STORE_TYPE_STATUS, Constant.TYPE_STORE_STATUS_PRVIEW_EDIT);

                } else {//3
                    intent.putExtra(Constant.KEY_STORE_TYPE_STATUS, Constant.TYPE_STORE_STATUS_EDIT);
//                    ciStore.setText(getString(R.string.un_cert),-1);
                }
                startActivityForResult(intent, 0x1992);

                break;
            case R.id.ci_bank:
                if (certStatus.equals("0") && certType == null) {//没有实名认证
                    toast("实名信息通过后可填写收款账号信息");
                    return;
                }/*else if ("3".equals(certStatus)){//审核被拒
                    toast("实名认证通过后可填写收款账号信息");
                    return;
                }*/
                MMKV.defaultMMKV().encode("isStore", true);
                Intent bank = new Intent(this, BankActivity.class);
//                bank.putExtra("card_count", cardCount);
//                bank.putExtra("type", 2);

                bank.putExtra("card_count", getStoreInfo().getBankCardCount());
//                bank.putExtra("card_count",0);
                String bankCardType = getStoreInfo().getBankCardType();

                if (bankCardType != null) {
                    if ("1".equals(bankCardType)) {//1个人,2企业
                        bank.putExtra(Constant.TYPE, -1);
                        bank.putExtra("userType", 1);
                    } else {
                        bank.putExtra(Constant.TYPE, 2);
                        bank.putExtra("userType", 2);

                    }
                } else {
                    if (certType.equals("1")) {//个人认证
                        bank.putExtra(Constant.TYPE, -1);
                        bank.putExtra("userType", 1);
                    } else {
                        bank.putExtra(Constant.TYPE, 2);
                        bank.putExtra("userType", 2);

                    }
                }
                bank.putExtra("isFirst", false);

                startActivity(bank);
//                //只有店铺存在才能添加银行卡
//                if (auditStatus.equals("0") || auditStatus.equals("1") || auditStatus.equals("2")) {
//
//                    //只有在身份认证的情况下才能添加银行卡
//                    if (certStatus.equals("1") || certStatus.equals("2")) {
//
//                        if (cardCount > 0){//已经绑定银行卡
//                            Intent bank = new Intent(this, BankActivity.class);
//                            bank.putExtra("card_count", cardCount);
//                            bank.putExtra(Constant.TYPE, -1);
//                            bank.putExtra("userType",1);
//                            startActivity(bank);
////                            if (certType == null) {//没有认证过
////                                toast(BaseApplication.getDoc().getCerficateFirst());
////                            } else if ("1".equals(bankCartType)) {//个人认证
////                                bank.putExtra(Constant.TYPE, -1);
////                                bank.putExtra("userType",1);
////                                startActivity(bank);
////                            } else if ("2".equals(bankCartType)) {//企业认证
////                                bank.putExtra(Constant.TYPE, 1);
////                                bank.putExtra("userType",2);
////                                startActivity(bank);
////                            }
//
//                        }else {
//                            Intent bank = new Intent(this, SelectNameEnsureActivity.class);
//                            bank.putExtra("card_count", cardCount);
//                            bank.putExtra("type", 2);
//                            startActivity(bank);
//                            if (certType == null) {//没有认证过
//                                toast(BaseApplication.getDoc().getCerficateFirst());
//                            } else {
////                                bank.putExtra("type", 2);
////                                startActivity(bank);
//                            }
//
//                        }
//
//                    } else {
//                        toast(BaseApplication.getDoc().getCerficateFirst());
//                    }
//
//                } else {
//                    toast(BaseApplication.getDoc().getCerficateFirst());
//                }

                break;
            case R.id.ci_start_amount:
                showStartMoneyDialog();
                break;
            case R.id.rightbtn:
                help();
                break;
        }
    }

    private void help() {

        Intent intent = new Intent(this, WebActivity.class);
        intent.putExtra(Constant.KEY_WEB_URL, Constant.URL_HELP_CREATED_STORE);
        intent.putExtra(Constant.KEY_WEB_TITLE, getString(R.string.help));
        startActivity(intent);

    }

    CustomDialog.Builder builder;

    private void dealSwitch() {
        if (builder == null) {
            builder = new CustomDialog.Builder(mContext);
            builder.setTitleVisible(false).setMessage("设置成不接单？")//设置显示的内容
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加确定按钮
                        @Override
                        public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                            dealSwitch(false);
                            dialog.dismiss();
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加返回按钮
                @Override
                public void onClick(DialogInterface dialog, int which) {//响应事件
                    dialog.dismiss();
                }

            });//在按键响应事件中显示此对话框
        }
        if (ssOrder.isOpen()) {
            builder.create().show();
        } else {
            dealSwitch(true);
        }

    }

    private void showStartMoneyDialog() {
        final IosAlertDialog iosAlertDialog = new IosAlertDialog(StoreEnsureAndOpenActivity.this);
        iosAlertDialog.builder().setTitle("请输入起送金额")
                .setEdit(true, "起送金额为大于等于0的整数", "起送金额不能为空", startMoney)
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startMoney = iosAlertDialog.getEditValue();
                        update_deliveryMinAmount();
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        }).show();
    }

    CustomDialog.Builder builder2;

    private void dealOffline() {
        if (builder2 == null) {
            builder2 = new CustomDialog.Builder(mContext);
            builder2.setTitleVisible(false).setMessage("设置线上不销售？")//设置显示的内容
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加确定按钮
                        @Override
                        public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                            dealOffline(false);
                            dialog.dismiss();
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加返回按钮
                @Override
                public void onClick(DialogInterface dialog, int which) {//响应事件
                    dialog.dismiss();
                }

            });//在按键响应事件中显示此对话框
        }
        if (ssOffline.isOpen()) {
            builder2.create().show();
        } else {
            dealOffline(true);
        }

    }

    public void dealSwitch(boolean isOpen) {

        RequestEntityMapSupply RequestEntitySupply = new RequestEntityMapSupply();
        RequestEntitySupply.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_isAcceptOrders);

        RequestEntitySupply.putParameter("isAcceptOrders", isOpen ? 1 : 0);
        RequestEntitySupply.putParameter("storeId", BaseApplication.getInstance().getStoreId());
        RequestEntitySupply.putParameter("merchantId", BaseApplication.getInstance().getMerchantId());
        request(RequestEntitySupply, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_isAcceptOrders, onDealSwitchRequestListener);
    }

    private OnRequestListener<StatusEntity> onDealSwitchRequestListener = new OnRequestListener<StatusEntity>() {
        @Override
        public StatusEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, StatusEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {

            hideProgressDialog();
            StatusEntity responseElement = response.getResponseElement();
            if (responseElement.isSuccess()) {
                boolean isOpen = !ssOrder.isOpen();
                ssOrder.setState(isOpen);
                StoreInfo storeInfo = getStoreInfo();
                if (storeInfo != null) {
                    Store store = storeInfo.getStore();
                    if (store != null) {
                        store.setAcceptOrders(isOpen);
                        updateStoreInfo(storeInfo);
                    }
                }
            } else {
                //没有数据
                toast("更改失败");
            }
        }
    };

    public void update_deliveryMinAmount() {
        RequestEntityMapSupply RequestEntitySupply = new RequestEntityMapSupply();
        RequestEntitySupply.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_deliveryMinAmount_update);
        RequestEntitySupply.putParameter("deliveryMinAmount", startMoney);
        request(RequestEntitySupply, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_deliveryMinAmount_update, deliveryMinAmountUpdateRequestListener);
    }


    private OnRequestListener<updateDeliveryMinAmountReponse> deliveryMinAmountUpdateRequestListener = new OnRequestListener<updateDeliveryMinAmountReponse>() {
        @Override
        public updateDeliveryMinAmountReponse jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, updateDeliveryMinAmountReponse.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<updateDeliveryMinAmountReponse> response, Object tag) {
            hideProgressDialog();
            updateDeliveryMinAmountReponse updateDeliveryMinAmountReponse = response.getResponseElement();
            if (updateDeliveryMinAmountReponse.isSuccess()) {
                ciStartAmount.setContentText(startMoney + "元");
            } else {
                T.toast("起送金额设置失败");
            }

        }
    };


    public void dealOffline(boolean isOpen) {

        RequestEntityMapSupply RequestEntitySupply = new RequestEntityMapSupply();
        RequestEntitySupply.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_modifySaleChannelApp);

        RequestEntitySupply.putParameter("saleChannelApp", isOpen ? 1 : 0);
        RequestEntitySupply.putParameter("storeId", BaseApplication.getInstance().getStoreId());
        RequestEntitySupply.putParameter("merchantId", BaseApplication.getInstance().getMerchantId());
        request(RequestEntitySupply, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_modifySaleChannelApp, onDealOfflineRequestListener);
    }

    private OnRequestListener<StatusEntity> onDealOfflineRequestListener = new OnRequestListener<StatusEntity>() {
        @Override
        public StatusEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, StatusEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {

            hideProgressDialog();
            StatusEntity responseElement = response.getResponseElement();
            if (responseElement.isSuccess()) {
                boolean isOpen = !ssOffline.isOpen();
                ssOffline.setState(isOpen);
                StoreInfo storeInfo = getStoreInfo();
                if (storeInfo != null) {
                    Store store = storeInfo.getStore();
                    if (store != null) {
                        store.setOffline(isOpen);
                        updateStoreInfo(storeInfo);
                    }
                }
            } else {
                //没有数据
                toast("更改失败");

            }

        }
    };


    @Override
    public void onFail(int failCode, Object arg, Object tag) {
        super.onFail(failCode, arg, tag);
        requestStoreInfo();
    }

    public void requestStoreInfo() {
        RequestEntityMapSupply RequestEntityMapSupply = new RequestEntityMapSupply();
        RequestEntityMapSupply.putParameter("merchantId", BaseApplication.getInstance().getMerchantId());
        RequestEntityMapSupply.putParameter("storeId", BaseApplication.getInstance().getStoreId());
        RequestEntityMapSupply.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_home4supplier);

        request(RequestEntityMapSupply, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_home4supplier, onRequestListener);
    }

    OnRequestListener<StoreInfo> onRequestListener = new OnRequestListener<StoreInfo>() {
        @Override
        public StoreInfo jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, StoreInfo.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {

            showExceptionView(R.mipmap.icon_error, "请求失败", -1);
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            showExceptionView(R.mipmap.icon_error, arg + "", -1);

        }

        @Override
        public void onStart(Object tag) {

            showProgressView();
        }

        @Override
        public void onResponse(ResponseEntity<StoreInfo> response, Object tag) {
            showMainView();
            StoreInfo responseElement = response.getResponseElement();
            updateStoreInfo(responseElement);
            updateStoreStatus(responseElement);
        }
    };


    public void getCertInfo() {
        RequestEntityMapSupply RequestEntitySupply = new RequestEntityMapSupply();
        RequestEntitySupply.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_account_merchant_cert);
        RequestEntitySupply.putParameter("merchantId", BaseApplication.getInstance().getMerchantId());
        RequestTag tag = new RequestTag();
        tag.type = RequestTag.TYPE_CURRENT;
        request(RequestEntitySupply, tag, onCertInfoRequestListener);

    }

    private OnRequestListener<CertInfo> onCertInfoRequestListener = new OnRequestListener<CertInfo>() {
        @Override
        public CertInfo jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, CertInfo.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            if (failCode == HttpRequest.FAIL_JSON_PARSER) {

            } else {
                toast(arg + "");
            }
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            toast(arg + "");
        }

        @Override
        public void onStart(Object tag) {

            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<CertInfo> response, Object tag) {

            hideProgressDialog();
            CertInfo ci = response.getResponseElement();

            intoCertView(ci);

        }
    };

    private void intoCertView(CertInfo ci) {
        if (ci != null) {
            if ("3".equals(certStatus)) {//认证失败
                Intent intent = new Intent(this,
                        SelectNameEnsureActivity.class);
                intent.putExtra("certInfo", new Gson().toJson(ci));
                intent.putExtra("isFirst", false);
                intent.putExtra("certType", certStatus);
                intent.putExtra("certPersonCompanyType", certType);
                startActivity(intent);

            } else if ("1".equals(certStatus)) {//认证中
                if ("0".equals(ci.getType())) {//个人认证
                    Intent intent = new Intent(this,
                            EnsurePersonActivity.class);
                    intent.putExtra("certInfo", new Gson().toJson(ci));
                    intent.putExtra("isFirst", false);
                    intent.putExtra("certType", certStatus);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(this,
                            EnsureCompanyActivity.class);
                    intent.putExtra("certInfo", new Gson().toJson(ci));
                    intent.putExtra("isFirst", false);
                    intent.putExtra("certType", certStatus);
                    startActivity(intent);
                }
            } else if ("2".equals(certStatus)) {//认证成功
                if ("0".equals(ci.getType())) {//个人认证
                    Intent intent = new Intent(this,
                            EnsurePersonActivity.class);
                    intent.putExtra("certInfo", new Gson().toJson(ci));
                    intent.putExtra("isFirst", false);
                    intent.putExtra("certType", certStatus);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(this,
                            EnsureCompanyActivity.class);
                    intent.putExtra("certInfo", new Gson().toJson(ci));
                    intent.putExtra("isFirst", false);
                    intent.putExtra("certType", certStatus);
                    startActivity(intent);
                }
            }

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x1992 && resultCode == RESULT_OK) {
            requestStoreInfo();
        }
        if (requestCode == 0x1995 && resultCode == RESULT_OK) {

            requestStoreInfo();
        }
    }


}
